package cn.fzu.cn.event;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

/**
 * ClassName : cn.fzu.cn.event.Test
 * Desc : TODO
 *
 * @author : LINPQ
 * @date : 2021/3/28 15:10
 */
public class Test {
    public static void main(String[] args) {

        JFrame jFrame = new JFrame();
        jFrame.setTitle("扫雷");
        jFrame.setLocation(500,500);
        jFrame.setSize(300,300);
        jFrame.setLayout(new BorderLayout());
        jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jFrame.setIconImage(new ImageIcon("./picture/icon.gif").getImage());
        jFrame.setResizable(false);
//        jFrame.pack();
        jFrame.setVisible(true);

        //事件源
        JLabel jLabel = new JLabel("AAA");
        JLabel jLabel2 = new JLabel("BBB");
        jFrame.add(jLabel);
        jFrame.add(jLabel2,BorderLayout.SOUTH);


        //创建事件监听
        LabelListener labelListener = new LabelListener();

        //绑定事件
        jLabel.addMouseListener(labelListener);
        jLabel2.addMouseListener(labelListener);



    }
}


class LabelListener implements  MouseListener {
    @Override
    public void mouseClicked(MouseEvent e) {

        JLabel label = (JLabel)e.getSource();
        System.out.println(label.getText());

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
